import { bindUserPhoneNumber, getUserInfo, updateUserInfo } from "../../../services/user";
import { phoneEncryption } from "../../../utils/index";

const app = getApp<IAppOption>();
const defaultAvatarUrl = "https://cdn-we-retail.ym.tencent.com/miniapp/usercenter/icon-user-center-avatar@2x.png";

Page({
    data: {
        userInfo: app.globalData.userInfo as any,
        showUnbindConfirm: false,
        pickerOptions: [
            {
                name: "男",
                code: "1",
            },
            {
                name: "女",
                code: "2",
            },
        ],
        typeVisible: false,
        genderMap: [
            { label: "", value: 0 },
            { label: "女", value: 1 },
            { label: "男", value: 2 },
        ],
    },

    onShow() {
        this.init();
    },

    init() {
        getUserInfo().then((userInfo: any) => {
            app.globalData.userInfo = userInfo;
            this.setData({
                userInfo,
                "userInfo.avatar": userInfo.avatar || defaultAvatarUrl,
                "userInfo.mobile": phoneEncryption(userInfo.mobile),
            });
        });
    },

    onClickCell({ currentTarget }: { currentTarget: any }) {
        const { dataset } = currentTarget;
        const { nickname } = this.data.userInfo;
        console.log("dataset", dataset);

        switch (dataset.type) {
            case "gender":
                this.setData({ typeVisible: true });
                break;
            case "name":
                wx.navigateTo({
                    url: `/pages/ucenter/name-edit/index?name=${nickname}`,
                });
                break;
            case "name":
                wx.navigateTo({
                    url: `/pages/ucenter/name-edit/index?name=${nickname}`,
                });
                break;
            default: {
                break;
            }
        }
    },

    onPickerChange(e: any) {
        const { value } = e.detail;
        this.setData({ "userInfo.gender": value[0] });
        updateUserInfo({ gender: value[0] });
    },

    onPickerCancel() {
        this.setData({ typeVisible: false });
    },

    onChooseAvatar(e: any) {
        const { avatarUrl } = e.detail;
        const sessionid = wx.getStorageSync("sessionid");
        const url = app.siteInfo.baseUrl + "/app/index.php?i=" + app.siteInfo.uniacid + "&c=wxapp&a=me&do=avatar&state=we7sid-" + sessionid;
        wx.uploadFile({
            url,
            filePath: avatarUrl,
            name: "file",
            header: {
                "X-Requested-With": "XMLHttpRequest",
            },
            success: () => {
                this.setData({ "userInfo.avatar": avatarUrl });
            },
            fail: (err) => {
                console.log(err); //发送失败回调，可以在这里了解失败原因
            },
        });
    },

    // 获取手机号
    getPhoneNumber(e: any) {
        if (e.detail.errMsg === "getPhoneNumber:ok") {
            bindUserPhoneNumber(e.detail.code);
        }
    },
});
